package com.atguigu.exam.service.impl;

import com.atguigu.exam.entity.AnswerRecord;
import com.atguigu.exam.entity.ExamRecord;
import com.atguigu.exam.entity.Paper;
import com.atguigu.exam.mapper.ExamRecordMapper;
import com.atguigu.exam.service.AnswerRecordService;
import com.atguigu.exam.service.ExamRecordService;
import com.atguigu.exam.service.ExamService;
import com.atguigu.exam.service.PaperService;
import com.atguigu.exam.vo.ExamRankingVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import kotlin.jvm.internal.Lambda;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 考试记录Service实现类
 * 实现考试记录相关的业务逻辑
 */
@Service
public class ExamRecordServiceImpl extends ServiceImpl<ExamRecordMapper, ExamRecord> implements ExamRecordService {

    @Autowired
    private PaperService paperService;

    @Autowired
    private AnswerRecordService answerRecordService;

    @Autowired
    private ExamRecordMapper examRecordMapper;

    /**
     * 获取考试排行榜
     *
     * @param paperId
     * @param limit
     * @return List<ExamRankingVO>
     */
    @Override
    public List<ExamRankingVO> findExamRanking(Integer paperId, Integer limit) {
        return examRecordMapper.selectExamRanking(paperId,limit);
    }

    /**
     * 根据Id删除考试记录
     * @param id
     */
    @Override
    public void removeExamRecordById(Integer id) {
        //判断考试记录的状态，如果是进行中不能删除
        ExamRecord examRecord = getById(id);
        if ("进行中".equals(examRecord.getStatus())){
            throw new RuntimeException("考试正在进行中，无法删除");
        }
        //删除考试记录
        //removeById(id);
        examRecordMapper.deleteById(id);
        //同时删除答题记录
        LambdaQueryWrapper<AnswerRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AnswerRecord::getExamRecordId,id);
        answerRecordService.remove(lambdaQueryWrapper);
    }

    /**
     * 查询考试记录
     * @param myPage
     * @param studentName
     * @param studentNumber
     * @param status
     * @param startDate
     * @param endDate
     */
    @Override
    public void queryExamRecord(Page<ExamRecord> myPage, String studentName, String studentNumber, Integer status, String startDate, String endDate) {
        //动态SQL语句
        LambdaQueryWrapper<ExamRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(studentName),ExamRecord::getStudentName,studentName);
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(status),ExamRecord::getStatus,status);
        if (status != null) {
            String statusStr = switch (status) {
                case 0 -> "进行中";
                case 1 -> "已完成";
                case 2 -> "已批阅";
                default -> null;
            };
            lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(statusStr),ExamRecord::getStatus, statusStr);
        }
        lambdaQueryWrapper.ge(!ObjectUtils.isEmpty(startDate),ExamRecord::getStartTime,startDate);
        lambdaQueryWrapper.le(!ObjectUtils.isEmpty(endDate),ExamRecord::getEndTime,endDate);
        page(myPage,lambdaQueryWrapper);
        //获取试卷名称
        //将分页查询结果中的exam_id转换为List集合
        List<Integer> paperIdList = myPage.getRecords().stream().map(ExamRecord::getExamId).collect(Collectors.toList());
        //查询在考试记录中的试卷
        LambdaQueryWrapper<Paper> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.in(!ObjectUtils.isEmpty(paperIdList),Paper::getId,paperIdList);
        List<Paper> paperList = paperService.list(lambdaQueryWrapper1);
        //将paperList转换为Map
        Map<Integer, Paper> paperMap = paperList.stream().collect(Collectors.toMap(paper->paper.getId().intValue(),paper->paper));
        //ExamRecord实体类中的paper属性赋值
        myPage.getRecords().forEach(examRecord->examRecord.setPaper(paperMap.get(examRecord.getExamId())));
    }
}


